package com.bezdelnick.forum.web.page;

import com.bezdelnick.forum.domain.ModelBean;
import com.bezdelnick.forum.domain.Question;
import com.bezdelnick.forum.domain.Subtype;
import com.bezdelnick.forum.domain.Type;
import com.bezdelnick.forum.service.ForumService;
import com.bezdelnick.forum.web.component.QuestionsListView;
import com.bezdelnick.forum.web.model.NavigationTreeModel;
import com.bezdelnick.forum.web.page.template.AdminMainMenuTemplate;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.tree.BaseTree;
import org.apache.wicket.markup.html.tree.LinkTree;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: User
 * Date: 10.08.12
 * Time: 14:13
 * To change this template use File | Settings | File Templates.
 */
@AuthorizeInstantiation("ROLE_MODER")
public class ModeratorPage extends AdminMainMenuTemplate {
    private static final Logger LOGGER = LoggerFactory.getLogger(ModeratorPage.class);
    private static final String TREE_ROOT_NAME = "Types answers";

    public static final String ACTION_EDIT = "edit";
    public static final String ACTION_REMOVE = "remove";

    public List<Question> questionList;

    @SpringBean
    @SuppressWarnings("unused")
    private ForumService forumService;

    private WebMarkupContainer container;
    private BaseTree navigationTree;
    private QuestionsListView questionsListView;

    public ModeratorPage() {
        this(new PageParameters());
    }

    public ModeratorPage(PageParameters pageParameters) {
        this.getPageParameters().mergeWith(pageParameters);
        if (!(this.getPageParameters().get("action").isNull() || this.getPageParameters().get("id").isNull())) {
            String deliveryAction = this.getPageParameters().get("action").toString();
            Long deliveryId = this.getPageParameters().get("id").toLong();

            if (forumService.getQuestionDao().getQuestionById(deliveryId) != null) {
                if (deliveryAction.equals(ACTION_REMOVE)) {
                    forumService.getAnswerDao().deleteAnswersByQuestionId(deliveryId);
                    forumService.getQuestionDao().deleteQuestion(deliveryId);
                }
            }
        }

        this.questionList = forumService.getQuestionDao().loadQuestions();

        container = new WebMarkupContainer("container");
        container.setOutputMarkupId(true);
        add(container);

        // add menu component
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(new ModelBean(TREE_ROOT_NAME));
        NavigationTreeModel navigationTreeModel = new NavigationTreeModel(rootNode);

        navigationTree = new LinkTree("menu", navigationTreeModel) {
            @Override
            protected void onNodeLinkClicked(Object node, BaseTree tree, AjaxRequestTarget target) {
                super.onNodeLinkClicked(node, tree, target);

                TreeNode _node = (TreeNode) node;

                if (!_node.isLeaf()) {
                    if (_node.toString().equals(TREE_ROOT_NAME)) {
                        questionList = forumService.getQuestionDao().loadQuestions();
                    } else {
                        Type selectedType = forumService.getTypeDao().getTypeByName(_node.toString());
                        questionList = forumService.getQuestionDao().loadQuestionsByTypeId(selectedType.getId());
                    }
                    tree.updateTree(target);
                } else {
                    Subtype selectedSubtype = forumService.getSubtypeDao().getSubtypeByName(_node.toString());
                    questionList = forumService.getQuestionDao().loadQuestionsBySubtypeId(selectedSubtype.getId());
                }
            }
        };

        container.add(navigationTree);
        navigationTree.add(new AjaxEventBehavior("onclick") {
            @Override
            protected void onEvent(AjaxRequestTarget target) {
                target.add(container);
            }
        });
        navigationTree.getTreeState().expandAll();

        // add body component
        questionsListView = new QuestionsListView("list_questions", new PropertyModel<List<Question>>(this, "questionList"), ForumService.MODE.MODER);
        questionsListView.setOutputMarkupId(true);
        container.add(questionsListView);
    }
}
